package org.paradise.dms.web.action.aptview;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.directwebremoting.annotations.RemoteProxy;
import org.paradise.dms.pojo.Apartment;
import org.paradise.dms.services.ApartmentService;
import org.paradise.dms.web.action.DMSBaseAction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dheaven.framework.dao.DaoException;

/**
 * 
 * Description: 实现公寓浏览的Action
 * 
 * 
 * Copyright (c) 2008-2009 paraDise sTudio(DT). All Rights Reserved.
 * 
 * @version 1.0 2009-3-18 上午01:32:43 李双江（paradise.lsj@gmail.com）created
 */
@RemoteProxy(name = "apartmentViewAction")
@Service
public class ApartmentViewAction extends DMSBaseAction {

	@SuppressWarnings("unused")
	private static Logger log = Logger.getLogger(ApartmentViewAction.class);
	/**
	 * 
	 */
	private static final long serialVersionUID = -2501095441138813910L;

	@Autowired
	private ApartmentService apartmentService;
	private List<Apartment> aptList;
	private List<Apartment> aptInfoList;

	public List<Apartment> getAptInfoList() {
		return aptInfoList;
	}

	public void setAptInfoList(List<Apartment> aptInfoList) {
		this.aptInfoList = aptInfoList;
	}

	private String apartmentid;

	/**
	 * 
	 * Description: 以图标的形式列出所有的公寓列表
	 * 
	 * @Version1.0 2009-3-18 上午01:34:05 李双江（paradise.lsj@gmail.com）创建
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String listAllApartment() {
		aptList = new ArrayList<Apartment>();
		aptList = apartmentService.listAllApartment();
		if (aptList != null && aptList.size() > 0) {
			String[] _aptyid = new String[aptList.size()];
			for (int m = 0; m < aptList.size(); m++) {
				_aptyid[m] = aptList.get(m).getApartmentid();
			}
			/**
			 * 查存储过程返回指定id宿舍的已住人数和可住人数
			 */
			// 目前只查1号楼的。没数据。同时没数据的时候total和avail都要为0，不能为null
			List lst = apartmentService
					.getTotalRoomsAndAvailRoomsByApartmentID(_aptyid);
			for (int k = 0; k < lst.size(); k++) {
				List li = (List) lst.get(k); // 每个存储过程查询出来的lst值只有一个
				// log.info(li.size());
				/**
				 * 此段主要用来处理根据公寓id去查存储过程时。某些项为空的情况处理 by lishuangjiang 3/21/2009
				 */
				for (int t = 0; t < li.size(); t++) {
					Object[] o = (Object[]) li.get(t);
					// log.info("object[]长度：" + o.length);
					if (o.length == 1) {
						aptList.get(k).setApartmenttotalrooms(o[0] + "");
						aptList.get(k).setApartmentavailrooms("0");
					} else {
						aptList.get(k).setApartmenttotalrooms(o[0] + "");
						aptList.get(k).setApartmentavailrooms(o[1] + "");
					}
				}
			}
			return SUCCESS;
		}
		return INPUT;
	}
/**
 * 获取ext用公寓的json数据
 * @return
 */
	public String getAptJSON() {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		int start;
		int limit;
		JSONArray jsonArray;
		int totalProperty = apartmentService.getAllApartmentRows();
		
		try {
			start = Integer.parseInt(request.getParameter("start"));
		} catch (NumberFormatException e1) {
			start = 0;
		}
		try {
			limit = Integer.parseInt(request.getParameter("limit"));
		} catch (NumberFormatException e1) {
			limit = 5;
		}
		//if (start > 0) {
		//	start = start + limit;
		//}
		aptInfoList = apartmentService.listAllApartmentByPage(limit, start);
		if (aptInfoList != null & aptInfoList.size() > 0) {
			jsonArray = JSONArray.fromObject(aptInfoList);
			response.setContentType("text/html;charset=UTF-8");// 解决中文乱码
			String jsonString = "{start:"+start+",limit:"+limit+",totalProperty:"+totalProperty+",infoList:" + jsonArray.toString() + "}";
			try {
				response.getWriter().write(jsonString);
			} catch (IOException e) {
				log.error(e);
			}
		}
		return null;
	}

	/**
	 * 
	 * Description: 页面下拉框选择公寓时，查询某个公寓详情
	 * 
	 * @Version1.0 2009-3-20 上午03:00:47 李双江（paradise.lsj@gmail.com）创建
	 * @param aptname
	 * @return
	 * @throws DaoException
	 * @throws UnsupportedEncodingException
	 */
	/*
	 * public String listApartmentByApartmentName() throws DaoException,
	 * UnsupportedEncodingException { aptList = new ArrayList<Apartment>();
	 * aptList = apartmentService.findApartmentByID(this.getApartmentid()); if
	 * (aptList != null && aptList.size() > 0) { return SUCCESS; } return INPUT; }
	 */
	/**
	 * 
	 * Description: 返回所有宿舍的信息（包括宿舍名称，id，宿舍的楼层数目<这个在apartview上要查询某楼层宿舍>）
	 * 
	 * @Version1.0 2009-3-24 下午09:41:11 李双江（paradise.lsj@gmail.com）创建
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public JSONArray getAllApartInfo() {
		aptInfoList = apartmentService.getAllApartInfo();
		if (aptInfoList != null & aptInfoList.size() > 0) {
			JSONArray jsonArray = JSONArray.fromObject(aptInfoList);
			return jsonArray;
		}
		return null;
	}

	// /**
	// *
	// * Description:返回楼层数
	// *
	// * @Version1.0 May 5, 2009 1:10:39 AM 李双江（paradise.lsj@gmail.com）创建
	// * @param cookiestr
	// * 以逗号分割的字符串
	// * @param l
	// * 字符串长度
	// * @param aprt_id
	// * 公寓楼id
	// * @return
	// */
	// public int getFloorNofromCookie(String cookiestr, int l, String aprt_id)
	// {
	// // 一号楼,1,11,二号楼,2,16,三号楼,3,14,四号楼,4,11,五号楼,5,12,六号楼,6,10,
	// // 七号楼,7,9,东门公寓,8,14,3#,10,2,老4斋,16,6
	// int num = 0;
	// log.info(6 % 3 != 0);
	// log.info(cookiestr + "------------");
	// log.info(aprt_id);
	// String[] st = cookiestr.split(",");
	// int i = 0;
	// do {
	// num = i;
	// for (i = num; i < st.length; i++) {
	// if (st[i] == aprt_id) {
	// log.info(i+"==for li mian d ");
	// continue;
	// }
	// }
	// } while ((i + 1) % 3 != 0);
	//
	// log.info(cookiestr.indexOf(aprt_id) + "!!");
	// log.info(cookiestr.substring(num, l).indexOf(aprt_id));
	// // do {
	// // num = cookiestr.substring(num, l).indexOf(aprt_id);
	// // log.info(num+"==返回num结果");
	// // } while ((num + 1) % 3 != 0); // 不是楼层数跳出循环
	// return num;
	// }

	public List<Apartment> getAptList() {
		return aptList;
	}

	public void setAptList(List<Apartment> aptList) {
		this.aptList = aptList;
	}

	public ApartmentService getApartmentService() {
		return apartmentService;
	}

	public void setApartmentService(ApartmentService apartmentService) {
		this.apartmentService = apartmentService;
	}

	public String getApartmentid() {
		return apartmentid;
	}

	public void setApartmentid(String apartmentid) {
		this.apartmentid = apartmentid;
	}
}
